package com.yy.wga.order.service.listener;

import com.yy.wga.order.service.pojo.OrderUpGradeDO;
import com.yy.wga.order.service.service.OrderUpGradeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
 * @Author: gc
 * @Date: 2024/10/26
 * @Description:
 **/
@Slf4j
@Component
public class PayCallbackEventListener implements ApplicationListener<PayCallbackEvent> {
    private final OrderUpGradeService orderUpGradeService;

    public PayCallbackEventListener(OrderUpGradeService orderUpGradeService) {
        this.orderUpGradeService = orderUpGradeService;
    }

    @Override
    public void onApplicationEvent(PayCallbackEvent event) {
        log.info("消费订单回调事件:{}", event.getOrderNo());
        // 修改spu订单状态
        OrderUpGradeDO update = new OrderUpGradeDO();
        update.setAppKey(event.getAppKey());
        update.setOrderNo(event.getOrderNo());
        update.setPayTime(event.getSuccessTime());
        Integer status;
        if (event.getStatus() != null) {
            // 支付状态 1、待支付 2、支付成功 3、支付失败
            if (event.getStatus() == 2) {
                status = 7;
            } else if (event.getStatus() == 3) {
                status = 1;
            } else {
                log.error("{}返回未知支付状态:{}", event.getOrderNo(), event.getStatus());
                return;
            }
            update.setStatus(status);
        }
        orderUpGradeService.updateInfo(update);
    }
}
